package com.lmx.persistence.service;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.google.common.collect.Maps;
import com.lmx.common.constants.CommonConstants;
import com.lmx.persistence.service.common.BaseCrudServiceImpl;
import com.lmx.repoDomain.exception.BusinessException;
import com.lmx.repoDomain.tmodel.RelCustomerScheme;
import com.lmx.repoDomain.tmodel.Scheme;

@Service(value = "schemeService")
public class SchemeServiceImpl extends BaseCrudServiceImpl<Scheme> implements
		SchemeService {

	@Override
	public void checkDelete(Scheme entity) {
		String queryStr = "from RelCustomerScheme where scheme.id= ?1";
		List<RelCustomerScheme> rels = super.em.createQuery(queryStr)
				.setParameter(1, entity.getId()).getResultList();
		if (rels != null && rels.size() > 0) {
			throw new BusinessException(null,
					CommonConstants.SCHEME_ERROR_CODE.NOT_DELETE.lable(),
					"不能删除");
		}
	}

	@Override
	public void checkRepeat(Scheme entity) {
		Map<String, Object> param = Maps.newHashMap();
		param.put("title", entity.getTitle());
		param.put("delStatus", CommonConstants.DEL_STATUS_NO_0);
		List<Scheme> schemes = find(param);
		if (schemes != null && schemes.size() > 0) {
			throw new BusinessException(null,
					CommonConstants.SCHEME_ERROR_CODE.INFO_REPEAT.lable(),
					"名称重复");
		}
	}
}
